#!/bin/bash
IMAGE_NAME=$(cat name)
VERSION=$(git symbolic-ref --short -q HEAD)
DATE=$(date "+%m%d")
## 判断当前日期是否已经有镜像
CURRENT_NUM=$(docker images | grep "${VERSION}-${DATE}" -c)
NUM=""
## 十位补0(正常情况不会一天打10个包)
if [ "${CURRENT_NUM}" -ne 0 ]; then
  COMMIT_ID_1=$(git rev-parse --short HEAD)
  TAG_1="${VERSION}-${DATE}.*-${COMMIT_ID_1}"
  GREP_OUTPUT="$(docker images | grep "${TAG_1}")"
  if [ -n "$GREP_OUTPUT" ] ; then
    echo "已有该镜像："
    echo "${GREP_OUTPUT}"
    exit 0
  fi
  NUM=$(("${CURRENT_NUM}"))
  NUM="0""${NUM}"
fi
COMMIT_ID=$(git rev-parse --short HEAD)
TAG="${VERSION}-${DATE}${NUM}-${COMMIT_ID}"
mvn clean package -Dmaven.test.skip=true docker:build -DimageName="${IMAGE_NAME}" -Dtag="${TAG}"
echo "镜像：${IMAGE_NAME}:${TAG}"